<template>
	<el-container class="home-container">
		<!-- 头部 -->
		<el-header>
			<div class="logo-wrap">
				<img src="https://dummyimage.com/100x100/e3e3e3/333" alt="">
				<span>电商后台管理系统</span>
			</div>
			<el-button type="info" @click="logout">退出</el-button>
		</el-header>
		<!-- 页面主体区 -->
		<el-container>
			<!-- 页面侧边栏 -->
			<el-aside :width="asideWidth">
				<div class="toggle-btn" @click="toggleCollapse">|||</div>
				<!-- 侧边栏菜单区域 -->
				<el-menu background-color="#313743" text-color="#fff" active-text-color="#339AFF" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
					<!-- 一级菜单 -->
					<!-- index只接收字符串所有我们用简单的拼接使数值变成字符串 -->
					<el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
						<!-- 一级菜单的模板区域 -->
						<template slot="title">
							<i :class="iconsObj[item.id]"></i>
							<span>{{item.authName}}</span>
						</template>
						<!-- 二级菜单 -->
						<el-menu-item :index="'/' + subItem.path + ''" v-for="subItem in item.children" :key="subItem.id" @click="savaPath('/' + subItem.path)">
							<template slot="title">
								<i class="el-icon-menu"></i>
								<span>{{subItem.authName}}</span>
							</template>
						</el-menu-item>
					</el-submenu>
				</el-menu>
			</el-aside>
			<!-- 右侧主题区-->
			<el-main>
				<router-view></router-view>
			</el-main>
		</el-container>
	</el-container>
</template>

<script>
	export default {
		data() {
			return {
				// 左侧菜单数据
				menuList: [],
				iconsObj: {
					125: "iconfont icon-user",
					103: "iconfont icon-tijikongjian",
					101: "iconfont icon-shangpin",
					102: "iconfont icon-danju",
					145: "iconfont icon-baobiao"
				},
				isCollapse: false,
				asideWidth: '200px',
				activePath: ''
			}
		},
		created() {
			this.getMenuList()
			// 当前活跃的路由地址
			this.activePath = window.sessionStorage.getItem("activePath")
		},
		methods: {
			// 退出登录
			logout() {
				// 清空token
				window.sessionStorage.clear();
				// 通过编程式导航跳转到登录页
				this.$router.push('/login');
			},
			// 获取所有菜单
			async getMenuList() {
				const data = await this.$http.get('menus')

				if (data.data.meta.status !== 200) {
					return this.$message.error(data.data.meta.msg)
				} else {
					this.menuList = data.data.data
				}

			},
			// 左侧菜单栏折叠
			toggleCollapse() {
				this.isCollapse = !this.isCollapse

				this.isCollapse ? this.asideWidth = '64px' : this.asideWidth = '200px'
			},
			// 保存当前活跃的路由
			savaPath(activePath) {
				window.sessionStorage.setItem("activePath", activePath)
				this.activePath = window.sessionStorage.getItem("activePath")
			}
		}
	}
</script>

<style lang="less" scoped>
	.home-container {
		height: 100%;
	}

	.el-header {
		background-color: #363D40;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.logo-wrap {
			box-sizing: border-box;
			height: 100%;
			display: flex;
			align-items: center;
			padding: 5px 0;
			color: #fff;
			font-size: 20px;

			img {
				padding: 8px;
				height: 100%;
				margin-right: 8px;
			}
		}
	}

	.el-aside {
		background-color: #313743;
		transition: all .3s;

		.toggle-btn {
			background-color: #475163;
			text-align: center;
			color: #fff;
			font-size: 10px;
			line-height: 24px;
			letter-spacing: 0.2em;
			cursor: pointer;
		}

		.el-menu {
			border-right: none;

			.el-submenu__title i {
				margin-right: 10px;
			}
		}
	}

	.el-main {
		background-color: #E9EDF1;
	}
</style>
